package cn.tianjindong.blog_portal.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogArticleComment;
import cn.tianjindong.blog.pojo.BlogMessageBoard;
import cn.tianjindong.blog.pojo.BlogUser;
import cn.tianjindong.blog_portal.service.ArticleService;
import cn.tianjindong.blog_portal.service.CommentService;
import cn.tianjindong.blog_portal.service.MessageService;
import cn.tianjindong.blog_portal.service.UserService;

@Controller
/**
 * 用户操作相关的Controller（更新用户信息、收藏文章、评论）
 * 
 * @author 田 金 东
 *
 */
public class UserController {
	@Autowired
	private UserService userService;

	@Autowired
	private CommentService commentService;

	@Autowired
	private MessageService messageService;

	@Autowired
	private ArticleService articleService;

	/**
	 * 在session中保存的BlogUser对象的key
	 */
	private static final String USER = "user";

	private static final String TO_LOGIN_PAGE = "redirect:/login.html";

	/**
	 * 跳转到用户信息页面
	 * 
	 * @return
	 */
	@RequestMapping("/user/user-info.html")
	public String toUserInfo() {
		return "/user-info";
	}

	/**
	 * 更新用户信息（Ajax请求）
	 * 
	 * @param session
	 * @param user
	 * @return
	 */
	@RequestMapping("/user/update.action")
	public @ResponseBody String updateUserInfo(HttpSession session, BlogUser user) {
		BlogUser sessionUser = (BlogUser) session.getAttribute(USER);
		if (sessionUser != null) {
			user.setId(sessionUser.getId());
			userService.updateUserInfo(user);
			session.setAttribute(USER, userService.getUserById(sessionUser.getId()));
		}
		return "ok";
	}

	/**
	 * 收藏文章
	 * 
	 * @return
	 */
	@RequestMapping("/user/collection/add.action")
	public @ResponseBody String collectArticle(HttpSession session, Long articleId) {
		BlogUser user = (BlogUser) session.getAttribute(USER);
		if (user != null) {
			try {
				userService.collectArticle(user.getId(), articleId);
				return "{\"status\":\"ok\"}";
			} catch (Exception e) {
				e.printStackTrace();
				return "{\"status\":\"error\"}";
			}
		}
		return null;
	}

	/**
	 * 收藏文章
	 * 
	 * @return
	 */
	@RequestMapping("/user/collection/delete.action")
	public @ResponseBody String unCollectArticle(HttpSession session, Long articleId) {
		BlogUser user = (BlogUser) session.getAttribute(USER);
		if (user != null) {
			try {
				userService.unCollectArticle(user.getId(), articleId);
				return "{\"status\":\"ok\"}";
			} catch (Exception e) {
				e.printStackTrace();
				return "{\"status\":\"error\"}";
			}
		}
		return null;
	}

	/**
	 * 页面加载时检查当前用户是否收藏该文章
	 * 
	 * @return
	 */
	@RequestMapping("/user/collection/check.action")
	public @ResponseBody String checkCollection(HttpSession session, Long articleId) {
		BlogUser user = (BlogUser) session.getAttribute(USER);
		if (user != null) {
			boolean tag = userService.checkArticleIsCollection(user.getId(), articleId);
			if (tag) {
				return "{\"status\":\"exist\"}";
			}
			return "{\"status\":\"unExist\"}";
		}
		return TO_LOGIN_PAGE;
	}

	/**
	 * 页面加载时检查当前用户是否收藏该文章
	 * 
	 * @return
	 */
	@RequestMapping("/user/comment/add.action")
	public @ResponseBody String addComment(HttpSession session, Long articleId, BlogArticleComment comment) {
		BlogUser user = (BlogUser) session.getAttribute(USER);
		if (user != null) {
			boolean tag = commentService.addComment(user.getId(), articleId, comment);
			if (tag) {
				return "{\"status\":\"ok\",\"content\":\"" + comment.getContent() + "\"}";
			}
		}
		return "{\"status\":\"error\"}";
	}

	/**
	 * 前往“问题反馈”页面
	 */
	@RequestMapping("/user/message-board.html")
	public String toMessageBoardPage() {
		return "/message-board";
	}

	/**
	 * 用户留言功能
	 * 
	 * @param message
	 * @return
	 */
	@RequestMapping("/user/message/add.action")
	public @ResponseBody String addMessage(HttpSession session, BlogMessageBoard message) {
		BlogUser user = (BlogUser) session.getAttribute(USER);
		if (user != null) {
			message.setUserId(user.getId());
			boolean tag = messageService.addMessage(message);
			if (tag) {
				return "{\"status\":\"ok\"}";
			}
			return "{\"status\":\"error\"}";
		}
		return null;
	}

	/**
	 * 前往个人收藏页面
	 * 
	 * @return
	 */
	@RequestMapping("/user/collection.html")
	public String toCollectionPage(HttpSession session, Model model,@RequestParam(defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "8") Integer countItem) {
		BlogUser user = (BlogUser) session.getAttribute(USER);
		if (user != null) {
			PageView<BlogArticle> pageView = articleService.getCollectionArticlePageView(user.getId(),currentPage,countItem);
			model.addAttribute("pageView", pageView);
			return "/collection";
		}
		return TO_LOGIN_PAGE;
	}
}
